package com.shanling.shanlingcontroller.music.player.playqueue


import android.util.Log
import com.shanling.shanlingcontroller.CustomApplication
import com.shanling.shanlingcontroller.R
import com.shanling.shanlingcontroller.bean.Constants
import com.shanling.shanlingcontroller.bean.EventCenter
import com.shanling.shanlingcontroller.music.event.PlayModeEvent
import com.shanling.shanlingcontroller.music.event.PlaylistEvent
import com.shanling.shanlingcontroller.utils.PreferenceUtil
import org.greenrobot.eventbus.EventBus

/**
 * Created by master on 2018/5/14.
 */

object PlayQueueManager {
    /**
     * 播放模式 0：顺序播放，1：单曲循环，2：随机播放
     */
    const val PLAY_MODE_OREDE= 0
    const val PLAY_MODE_REPEAT = 1
    const val PLAY_MODE_RANDOM = 2
    const val PLAY_MODE_LOOP = 3

    //播放模式
    private var playingModeId = 0


    private val playingMode = arrayOf( R.string.play_mode_in_order,R.string.play_mode_repeat, R.string.play_mode_random,R.string.play_mode_loop)

    /**
     * 总共多少首歌曲
     */
    private var total = 0
    private var orderList = mutableListOf<Int>()
    private var saveList = mutableListOf<Int>()
    private var randomPosition = 0


    /**
     * 更新播放模式
     */
    fun updatePlayMode(): Int {
        playingModeId = (playingModeId + 1) % 4
       PreferenceUtil.getInstance(CustomApplication.getInstance()).setPlayMode(playingModeId);
        EventBus.getDefault().post(EventCenter(Constants.PLAYMODEEVENT, PlayModeEvent()))
        return playingModeId
    }

    /**
     * 获取播放模式id
     */
    fun getPlayModeId(): Int {
        playingModeId = PreferenceUtil.getInstance(CustomApplication.getInstance()).getPlayMode()
        return playingModeId
    }

    /**
     * 获取播放模式
     */
    fun getPlayMode(): String {
        return CustomApplication.getInstance().getString(playingMode[playingModeId]);
    }

    private fun initOrderList(total: Int) {
        this.total = total
        orderList.clear()
        for (i in 0 until total) {
            orderList.add(i)
        }

        /**
         * 更新
         */
        if (getPlayModeId() == PLAY_MODE_RANDOM) {
        //    orderList.shuffle()
            randomPosition = 0
            printOrderList(-1)
        }
    }

    /**
     * 获取下一首位置
     *
     * @return isAuto 是否自动下一曲
     */
    fun getNextPosition(intercept: Boolean?, total: Int, cuePosition: Int): Int {
        if (total == 1) {
            return 0
        }
       var id =getPlayModeId()
        initOrderList(total)
        if (id == PlayQueueManager.PLAY_MODE_REPEAT ) {
            if(intercept!!){
                return if (cuePosition < 0) {
                    0
                } else {
                    cuePosition
                }
            }else{
                if (cuePosition == total - 1) {
                    return 0
                } else if (cuePosition < total - 1) {
                    return cuePosition + 1
                }
            }
        } else if (id == PlayQueueManager.PLAY_MODE_RANDOM) {
            printOrderList(orderList[randomPosition])
            saveList.add(orderList[randomPosition])
            return orderList[randomPosition]
        }else if(id == PlayQueueManager.PLAY_MODE_LOOP){
            if (cuePosition == total - 1) {
                return 0
            } else if (cuePosition < total - 1) {
                return cuePosition + 1
            }
        }
        else {
            return cuePosition + 1
        }
        return cuePosition
    }

    /**
     *
     *
     * @return isAuto 是否自动下一曲
     */
    fun getPreviousPosition(intercept: Boolean?,total: Int, cuePosition: Int): Int {
        if (total == 1) {
            return 0
        }
        var id =getPlayModeId()
        if (id== PlayQueueManager.PLAY_MODE_REPEAT) {
            if(intercept!!){
                return if (cuePosition < 0) {
                    0
                } else {
                    cuePosition
                }
            }else{
                if (cuePosition == 0) {
                    return total - 1
                } else if (cuePosition > 0) {
                    return cuePosition - 1
                }
            }
        } else if (id == PlayQueueManager.PLAY_MODE_RANDOM) {
            randomPosition = if (saveList.size > 0) {
                saveList.last()
                saveList.removeAt(saveList.lastIndex)
            } else {
                randomPosition--
                if (randomPosition < 0) {
                    randomPosition = total - 1
                }
                orderList[randomPosition]
            }
            printOrderList(randomPosition)
            return randomPosition
        } else if(id== PlayQueueManager.PLAY_MODE_LOOP){
            if (cuePosition == 0) {
                return total - 1
            } else if (cuePosition > 0) {
                return cuePosition - 1
            }
        }else{
            return cuePosition - 1
        }
        return cuePosition
    }

    /**
     * 打印当前顺序
     */
    private fun printOrderList(cur: Int) {

    }
}
